<?php

namespace App\Kuafu\Brand\Controller\B;

use App\Kuafu\Base\Controller\BaseController;
use App\Kuafu\Brand\Enum\BrandCodeEnum;
use App\Kuafu\Brand\Service\BrandService;
use App\Kuafu\Brand\Validate\BrandValidate;
use App\Kuafu\Common\Service\CommonService;
use Illuminate\Http\Request;

class BrandController extends BaseController
{

    /**
     * 添加品牌
     * @param Request $request
     */
    public function addBrand(Request $request)
    {
        $valid = BrandValidate::addOrSetBrand($request);
        if ($valid) {
            return $this->fail(BrandCodeEnum::ERR_400000, '添加失败');
        }
        BrandService::addBrand($request);
        return $this->success();
    }

    /**
     * 获取品牌列表
     * @param Request $request
     */
    public function getBrandList(Request $request)
    {
        $res = BrandService::getBrandList($request);
        return $this->success($res);
    }

    /**
     * 获取品牌详情
     * @param Request $request
     */
    public function getBrandInfo(Request $request)
    {
        if (empty($request->brandId)) {
            return $this->fail(BrandCodeEnum::ERR_400000, "非法数据,参数错误");
        }
        $brandInfo  = BrandService::getBrandInfo($request);
        $returnData = null;
        if ($brandInfo) {
            $returnData = [
                'id'        => $brandInfo['id'],
                'name'      => $brandInfo['name'],
                'imageUrl'  => CommonService::joinImageUrl($brandInfo['imageUrl']),
                'status'    => $brandInfo['status'],
                'createdAt' => $brandInfo['createdAt'],
            ];
        }
        return $this->success($returnData);
    }

    /**
     * 修改品牌信息
     * @param Request $request
     */
    public function setBrandInfo(Request $request)
    {
        $valid = BrandValidate::addOrSetBrand($request);
        if ($valid) {
            return $this->fail(BrandCodeEnum::ERR_400000, $valid);
        }
        BrandService::setBrandInfo($request);
        return $this->success();
    }

    /**
     * 删除品牌（软删除）
     * @param Request $request
     */
    public function isDeletedBrand(Request $request)
    {
        if (empty($request->brandId)) {
            return $this->fail(BrandCodeEnum::ERR_400000, "非法数据,参数错误");
        }
        if (!in_array($request->type, [0, 1])) {
            return $this->fail(BrandCodeEnum::ERR_400000, "非法数据,参数错误");
        }
        BrandService::isDeletedBrand($request);
        return $this->success();
    }

    /**
     * 品牌状态
     * @param Request $request
     */
    public function setBrandStatus(Request $request)
    {
        if (empty($request->brandId)) {
            return $this->fail(BrandCodeEnum::ERR_400000, "非法数据,参数错误");
        }
        if (!is_int($request->status)) {
            return $this->fail(BrandCodeEnum::ERR_400000, "非法数据,参数错误");
        }
        BrandService::setBrandStatus($request);
        return $this->success();
    }
}
